﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Freeform;

namespace Space_in_Shaders
{
    public class PlayerShip : SpaceShip
    {

        public Single Thrust { get; set; }

        public Boolean LeftThrust { get; set; }
        public Boolean UpThrust { get; set; }
        public Boolean DownThrust { get; set; }
        public Boolean RightThrust { get; set; }

        public PlayerShip(SpaceWorld w)
            :base (w)
        {
            Game.LoadAnimation(this);
            Actor.Depth = 0.2f;
            SpatialProperties.Size = new Vector2(9, 12);
            PuffSpacing = 100;
            NextPuff = PuffSpacing;
            Thrust = 140f;
            LaserSpacing = 120;
            NextLaser = LaserSpacing;
            Game.KeyBinder.BindPressed += HandleInput;
            Game.KeyBinder.BindReleased += HandleInput;
            Health = 20;
            ExplosionIntensity = 12;

            CollisionProperties.CollisionID = RadialCollision.CID_PLAYERSHIP;
            Radial.Radius = 6;
            CollisionProperties.SetCollisionMask(RadialCollision.CID_AISHIP, true);
            CollisionProperties.SetCollisionMask(RadialCollision.CID_LASER, true);
            CollisionProperties.SetCollisionMask(RadialCollision.CID_MISSILE, true);
        }

        public override void Update(GameTime time)
        {
            // Check if the ship is thrusting at all so the base class can create smoke trail.
            IsThrusting = UpThrust || DownThrust || LeftThrust || RightThrust;

            base.Update(time);

            // Set the direction of ship facing the mouse pointer.
            Vector2 MouseWorld = World.Viewports[0].MouseLocation_World;
            Vector2 Diff = MouseWorld - SpatialProperties.Position;
            SpatialProperties.Direction = (Single)Math.Atan2(Diff.X, -Diff.Y);

            // Apply thruster forces.
            if (UpThrust)
                NMot.ApplyForce(Vector2.UnitY * -Thrust);
            if (DownThrust)
                NMot.ApplyForce(Vector2.UnitY * Thrust);
            if (LeftThrust)
                NMot.ApplyForce(Vector2.UnitX * -Thrust);
            if (RightThrust)
                NMot.ApplyForce(Vector2.UnitX * Thrust);


            // Check if player if firing laser or missile.
            IsFiringLaser = Game.Input.MouseState_Current.LeftButton == ButtonState.Pressed;
            IsFiringMissile = Game.Input.MouseState_Current.RightButton == ButtonState.Pressed;

            // Lock position inside game bounds.
            if (SpatialProperties.Position.X < SpaceWorld.BoundaryLeft)
            {
                SpatialProperties.Position = new Vector2(SpaceWorld.BoundaryLeft, SpatialProperties.Position.Y);
                NMot.Velocity = new Vector2(-NMot.Velocity.X, NMot.Velocity.Y);
            }
            else if (SpatialProperties.Position.X > SpaceWorld.BoundaryRight)
            {
                SpatialProperties.Position = new Vector2(SpaceWorld.BoundaryRight, SpatialProperties.Position.Y);
                NMot.Velocity = new Vector2(-NMot.Velocity.X, NMot.Velocity.Y);
            }
            if (SpatialProperties.Position.Y < SpaceWorld.BoundaryTop)
            {
                SpatialProperties.Position = new Vector2(SpatialProperties.Position.X, SpaceWorld.BoundaryTop);
                NMot.Velocity = new Vector2(NMot.Velocity.X, -NMot.Velocity.Y);
            }
            else if (SpatialProperties.Position.Y > SpaceWorld.BoundaryBottom)
            {
                SpatialProperties.Position = new Vector2(SpatialProperties.Position.X, SpaceWorld.BoundaryBottom);
                NMot.Velocity = new Vector2(NMot.Velocity.X, -NMot.Velocity.Y);
            }

            // Switch between Firing and Idle animations.
            if (IsFiringLaser && ImageActor.Animations.Current.AnimationName != "Firing")
                ImageActor.Animations.PlayAnimation("Firing");
            else if (!IsFiringLaser && ImageActor.Animations.Current.AnimationName != "Normal")
                ImageActor.Animations.PlayAnimation("Normal");
        }


        private void HandleInput(KeyBinding Bind)
        {
            // Check which bind was fired.
            switch (Bind.Name)
            {
                case "Up": UpThrust = Bind.State == KeyState.Down; break;
                case "Down": DownThrust = Bind.State == KeyState.Down; break;
                case "Left":  LeftThrust = Bind.State == KeyState.Down; break;
                case "Right":  RightThrust = Bind.State == KeyState.Down; break;
            }
        }

        public override void CollidesWith(SpaceObject Other)
        {
            base.CollidesWith(Other);

            SpaceWorld.View.ColourOverlay = Color.Red;
        }
    }
}
